import request from '@/request'


export function changeArticleViewStatus(token, id, newStatus){
  return request({
    headers: {'Authorization': token},
    url: `articles/admin/${id}/${newStatus}`,
    method: 'post'
  })
  
}

export function deleteArticle(token, id){
  return request({
    headers: {'Authorization': token},
    url: `articles/admin/delete/${id}`,
    method: 'post'
  })
  
}

export function getArticles(query, page) {
  return request({
    url: '/articles',
    method: 'post',
    data: {
      page: page.pageNumber,
      pageSize: page.pageSize,
      name: page.name,
      sort: page.sort,
      year: query.year,
      month: query.month,
      tagId: query.tagId,
      categoryId: query.categoryId
      
    }
  })
}

export function getArticlesForAdmin(token, page, otherData) {
  return request({
    headers: {'Authorization': token},
    url: '/articles/admin',
    method: 'post',
    data: {
      page: page.pageNumber,
      pageSize: page.pageSize,
      name: page.name,
      sort: page.sort,
      title: otherData.title,
      authorId: otherData.author,
      timeStart: otherData.timeStart,
      timeEnd: otherData.timeEnd,
      categoryId: otherData.category
    }
  })
}

export function getHotArtices() {
  return request({
    url: '/articles/hot',
    method: 'get'
  })
}

export function getNewArtices() {
  return request({
    url: '/articles/new',
    method: 'get'
  })
}

export function viewArticle(id, isView) {
  if(isView != null && isView){
    return request({
      url: `/articles/view/${id}?view=1`,
      method: 'get'
    })
  }else{
    return request({
      url: `/articles/view/${id}`,
      method: 'get'
    })
  }
  
}

export function getArticlesByCategory(id) {
  return request({
    url: `/articles/category/${id}`,
    method: 'get'
  })
}

export function getArticlesByTag(id) {
  return request({
    url: `/articles/tag/${id}`,
    method: 'get'
  })
}


export function publishArticle(article,token) {
  return request({
    headers: {'Authorization': token},
    url: '/articles/publish',
    method: 'post',
    data: article
  })
}

export function searchArticle(searchPar) {
  //避免刚刚点击输入框还没来得及输入内容时：空参异常
  if(searchPar == null || searchPar.length < 1){
    return;
  }
  searchPar += '';
  return request({
    url: `/articles/search/${searchPar}`,
    method: 'post',
    // data: {"search":search}
  })
}

export function listArchives() {
  return request({
    url: '/articles/listArchives',
    method: 'get'
  })
}

export function getArticleById(id) {
  return request({
    url: `/articles/${id}`,
    method: 'post'
  })
}
